package com.sumer.psychology.space.infrastructure.persistence.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sumer.psychology.blog.domain.model.BlogAvailability;
import com.sumer.psychology.blog.infrastructure.persistence.mongo.MongoBlogRepository;
import com.sumer.psychology.microblog.infrastructure.persistence.mongo.MongoMicroBlogRepository;
import com.sumer.psychology.photo.infrastructure.persistence.mongo.MongoPhotoRepository;
import com.sumer.psychology.sharing.infrastructure.persistence.mongo.MongoSharedResourceRepository;
import com.sumer.psychology.space.domain.MessageType;

/**
 * mongo评论消息资源库
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/11/5
 * Time: 9:57
 */
public class MongoCommentMessageRepository extends MongoMessageRepository {
    private MongoBlogRepository mongoBlogRepository;
    private MongoMicroBlogRepository mongoMicroBlogRepository;
    private MongoPhotoRepository mongoPhotoRepository;
    private MongoSharedResourceRepository mongoSharedResourceRepository;

    @Override
    public void add(String data) {
        DBObject comment = initializeDataWithNewKey(data, "commentId.identity", "commentId");
        String hostId = nestedValue(comment, "host.identity");
        String commentType = (String) comment.get("commentType");

        DBObject host = host(commentType, hostId);
        ((DBObject) comment.get("host")).put("name", host.get("hostName"));
        host.removeField("hostName");

        comment.put("messageType", MessageType.COMMENT.toString());
        update(host, comment);
    }

    /*
     * 获得宿主信息
     */
    private DBObject host(String commentType, String hostId) {
        String hostName = "";
        DBObject author = null;

        switch (commentType) {
            case "BLOG":
                DBObject blog = mongoBlogRepository().findOne(hostId, BlogAvailability.ACTIVE);
                author = (DBObject) blog.get("author");
                hostName = (String) blog.get("title");
                break;
            case "PHOTO":
                DBObject photo = mongoPhotoRepository().findOne(hostId);
                author = (DBObject) photo.get("author");
                hostName = (String) photo.get("title");
                break;
            case "RESOURCE":
                DBObject resource = mongoSharedResourceRepository().findOne(hostId);
                author = (DBObject) resource.get("author");
                hostName = (String) resource.get("topic");
                break;
            case "MICRO_BLOG":
                DBObject microBlog = mongoMicroBlogRepository().findOne(hostId);
                author = (DBObject) microBlog.get("author");
                break;
        }

        if (author != null) {
            return new BasicDBObject("_id", author.get("identity"))
                    .append("owner_name", author.get("name"))
                    .append("hostName", hostName);
        }
        return null;
    }

    public void setMongoBlogRepository(MongoBlogRepository mongoBlogRepository) {
        this.mongoBlogRepository = mongoBlogRepository;
    }

    public void setMongoMicroBlogRepository(MongoMicroBlogRepository mongoMicroBlogRepository) {
        this.mongoMicroBlogRepository = mongoMicroBlogRepository;
    }

    public void setMongoPhotoRepository(MongoPhotoRepository mongoPhotoRepository) {
        this.mongoPhotoRepository = mongoPhotoRepository;
    }

    public void setMongoSharedResourceRepository(MongoSharedResourceRepository mongoSharedResourceRepository) {
        this.mongoSharedResourceRepository = mongoSharedResourceRepository;
    }

    private MongoBlogRepository mongoBlogRepository() {
        return mongoBlogRepository;
    }

    private MongoMicroBlogRepository mongoMicroBlogRepository() {
        return mongoMicroBlogRepository;
    }

    private MongoPhotoRepository mongoPhotoRepository() {
        return mongoPhotoRepository;
    }

    private MongoSharedResourceRepository mongoSharedResourceRepository() {
        return mongoSharedResourceRepository;
    }
}
